function isArray(testObject) {   
    return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number';
}

exports.onRequest = (function(template) {
    
    return function (request, response, hostConfig) {
        console.log("starting request");
        var url = require('url').parse(request.url, true);
        var pathname = url.pathname.replace(/\.\./g, "").replace(/^\//, "");
        if (!pathname) {
            pathname = hostConfig.defaultPage;
        }
        pathname = (hostConfig.baseDirectory) + pathname;
        console.log(pathname);
        var fs = require("fs");
        fs.readFile(pathname, "utf-8", function (err, data) {
            if (err) {
                console.log("ERR");
            }
            template = JSON.parse(data); 
            var headerWritten = false, 
                writeHeader = function(responseCode, headers) {
                    if (headerWritten) {
                        return;
                    }
                    headerWritten = true;
                    response.writeHead(responseCode||200, headers||{"content-type":"text/html"});
                },
                write = function(data, encoding, statusCode) {
                    console.log(data);
                    writeHeader(statusCode||template?template.statusCode:0, template?template.headers:null);
                    response.write(data, encoding||"utf-8");
                },
                walkTemplate = function(t) {
                    if (typeof t === "string") {
                        write(t);
                        return;
                    }
                    console.log(JSON.stringify(t));
                    for (var leaf in t) {
                        if (!Object[leaf]) {
                            if (typeof t[leaf] === "string") {
                                write(t[leaf]);
                                continue;
                            }
                            if (t[leaf].tag) {
                                write("<" + t[leaf].tag);
                                if (t[leaf].class) {
                                    write(" class=\"" + t[leaf].class + "\"");
                                }
                                write(">");
                            }
                            if (isArray(t[leaf].children)) {
                                walkTemplate(t[leaf].children);
                            }
                            if (t[leaf].tag) {
                                write("</" + t[leaf].tag + ">");
                            }
                        }
                    }
                };
            
            if (err) {
                console.log("ERR");
                response.write(JSON.stringify(err), "ascii", 404);
            } else {
                walkTemplate({"body" : template.body});
            }
            response.end();
        });
    };

})({ "body" : {
            "tag" : "h1",
            "children" : [
                    "Hello World"
                ]
        }});